package com.suxii.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suxii.mapper.ChatFriendMapper;
import com.suxii.pojo.ChatFriend;
import com.suxii.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FriendServiceImpl extends ServiceImpl<ChatFriendMapper, ChatFriend> implements FriendService{

    @Autowired
    private ChatFriendMapper chatFriendMapper;

    @Override
    public List<Integer> friend(ChatFriend chatFriend) {
        var res=lambdaQuery()
                .select(ChatFriend::getUuid)
                .eq(ChatFriend::getUid,chatFriend.getUid())
                .list();
        List<Integer> s=new ArrayList<>();
        res.forEach((u)->s.add(u.getUuid()));
        return s;
    }

    @Override
    public void addFriend(ChatFriend chatFriend) {
        boolean f1 = lambdaQuery().select(ChatFriend::getUid)
                .eq(ChatFriend::getUid, chatFriend.getUid())
                .eq(ChatFriend::getUuid, chatFriend.getUuid())
                .count() > 0;
        boolean f2 = lambdaQuery().select(ChatFriend::getUid)
                .eq(ChatFriend::getUuid, chatFriend.getUid())
                .eq(ChatFriend::getUid, chatFriend.getUuid())
                .count() > 0;
        if (!(f1&&f2)) {
            chatFriendMapper.insert(new ChatFriend(chatFriend.getUid(), chatFriend.getUuid()));
            chatFriendMapper.insert(new ChatFriend(chatFriend.getUuid(), chatFriend.getUid()));
        }
    }

    @Override
    public void deleteFriend(ChatFriend chatFriend) {
        Map<String,Object> map=new HashMap<>();
        map.put("uid",chatFriend.getUid());
        map.put("uuid",chatFriend.getUuid());
        chatFriendMapper.deleteByMap(map);
        map.put("uuid",chatFriend.getUid());
        map.put("uid",chatFriend.getUuid());
        chatFriendMapper.deleteByMap(map);
    }
}
